package com.hxzy.controller;


import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.page.TableDataInfo;
import com.hxzy.dto.HouseTypesSearchDTO;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.HouseTypes;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.HouseTypesService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class HouseTypesController extends BaseController {

    @Autowired
    private HouseTypesService houseTypesService;

    @GetMapping("/house/types/list")
    private String showPage(Model model) {
        return "house/types/list";
    }

    //@RequiresPermissions(value = "house:types:list")
    @ResponseBody
    @PostMapping("/house/types/list")
    public TableDataInfo ajaxData(HouseTypesSearchDTO houseTypesSearchDTO, HttpServletRequest request){

        //分页
        super.startPage(request);

        List<HouseTypes> houseTypes = this.houseTypesService.search(houseTypesSearchDTO);

        return getDataTable(houseTypes);
    }


    @GetMapping("/house/types/add")
    public String showAdd(Model model){
        return "house/types/add";
    }

    @ResponseBody
    @PostMapping("/house/types/add")
    public AjaxResult saveAdd(Model model, HouseTypes houseTypes){

        AdminUser adminUser = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        houseTypes.setCreateTime(new Date());
        houseTypes.setUpdateTime(new Date());
        houseTypes.setCreateBy(adminUser.getLoginUserName());
        houseTypes.setUpdateBy(adminUser.getLoginUserName());

        int insert = this.houseTypesService.insert(houseTypes);

        return toAjax(insert);
    }


    @GetMapping("/house/types/edit/{id}")
    public String showEdit(@PathVariable("id")Integer id, Model model){

        HouseTypes houseTypes = this.houseTypesService.selectByPrimaryKey(id);

        model.addAttribute("houseTypes", houseTypes);

        return "house/types/edit";
    }

    @ResponseBody
    @PostMapping("/house/types/edit")
    public AjaxResult saveEdit(Model model, HouseTypes houseTypes){

        AdminUser adminUser = (AdminUser) SecurityUtils.getSubject().getPrincipal();

        houseTypes.setUpdateTime(new Date());
        houseTypes.setUpdateBy(adminUser.getLoginUserName());

        int insert = this.houseTypesService.updateByPrimaryKeySelective(houseTypes);

        return toAjax(insert);
    }

    @ResponseBody
    @GetMapping("/house/types/exists")
    public Integer existsName(Integer id,String name){

       int count = this.houseTypesService.existsName(id, name);

       return count;
    }
}
